package org.nokatag.tagjava;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.noka.gson.Gson;
import org.nokatag.dbgrid.GridBoxItem;
/**
 * 弹出框
 * @author xiefangjian
 *
 */
public class NokaBox extends NokaTag{
	private static final long serialVersionUID = 4886974242332352613L;
	private String width=null;//宽
	private String height=null;//高
	private String lotype = "iframe";//加载类型
	private String onopen=null;//方法名
	private String onclose=null;//方法名
	private String id = null;//控件ID
	private String title = null;//标题
	private String showtitle = "true";//是否显示标题
	private String stopclose = null;//禁用点击外部空白处关闭弹框
	
	private Set<GridBoxItem> GridIds = null;
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		StringBuilder stringBuilder = new StringBuilder();
		GridIds = new HashSet<GridBoxItem>();//Grid
		stringBuilder.append(potjs(request));//js框架
		stringBuilder.append(nkBoxJsAndCss(request));
		stringBuilder.append("<div id=\""+id+"\" style=\"display:block;\" class=\"initboxdiv\">");
		if("html".equals(lotype)){
			stringBuilder.append("<div class=\"tmask initboxdiv\" id=\""+id+"_boxid_tmask\" style=\"opacity: 0.7; display: block;z-index:999999\"></div>");
			stringBuilder.append("<div class=\"tbox\" id=\""+id+"_boxid_tbox\" style=\"position: absolute; top: 32px; left: -95.5px; opacity: 1; display: block;z-index:9999999\">");
			stringBuilder.append("<div class=\"tinner\"  id=\""+id+"_boxid_tinner\" style=\" background-image: none;\">");
			stringBuilder.append("<div class=\"tcontent initboxdiv\" id=\""+id+"_boxid_tcontent\">");
			if("true".equals(showtitle)){
				stringBuilder.append("<div class=\"tiny_title\" id=\""+id+"_boxid_tite\" >"+(null==title?"":title)+"</div>");
			}
		}
		try {
			jspOut.write(stringBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	public int doEndTag() throws JspException {
		try {
			StringBuilder body = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			String basePath = basepath(request);
			String BHeight=LenNumber(height);
			String BWidth=LenNumber(width);
			if("html".equals(lotype)){
				if("true".equals(showtitle)){
					body.append("<div  id=\""+id+"_boxid_tclose\" class=\"tclose\"></div>");
				}else{
					body.append("<div  id=\""+id+"_boxid_tclose\" style=\"display: none;\" class=\"tclose\"></div>");
				}
				body.append(" </div>");
				body.append("</div>");
				body.append("</div>");
			}
			body.append("</div>");
			body.append("<script type=\"text/javascript\">");
			//---------------显示树形菜单---------------------------
			body.append("Event.observe(document, 'dom:loaded', function() {");
			body.append("var sfg"+id+" = {");
			body.append("id:'"+id+"',");//id
			body.append("stopclose:"+(null==stopclose?"false":stopclose)+",");
			body.append("rurl:'"+basePath+"',");//根url
			body.append("bheight:"+BHeight+",");//弹出框高
			body.append("bwidth:"+BWidth+",");//弹出框宽
			body.append("lotype:'"+lotype+"',");
			if(null!=title){
				body.append("title:'"+title+"',");
			}
			if("true".equals(showtitle)){
				body.append("showtitle:true,");
			}else{
				body.append("showtitle:false,");
			}
			if(!GridIds.isEmpty()){
				try{
					Gson gson = new Gson();
					body.append("gridids:"+gson.toJson(GridIds)+",");
				}catch(Exception se){}
			}
			body.append("openfunction:function(){"+(onopen==null?"":""+onopen+"();")+"},");//onchange
			body.append("closefunction:function(){"+(onclose==null?"":""+onclose+"();")+"}");//onchange
			body.append("};");
			body.append("var nr"+id+" = new nbox(sfg"+id+");");
			body.append("nr"+id+".show();");
			body.append("});");
			body.append("</script>");
			pageContext.getOut().write(body.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		return EVAL_PAGE;
	}
	
	public void addGrid(GridBoxItem grid){
		GridIds.add(grid);
	}
	
	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}
	
	public String getLotype() {
		return lotype;
	}
	public void setLotype(String lotype) {
		this.lotype = lotype;
	}
	public String getOnopen() {
		return onopen;
	}
	public void setOnopen(String onopen) {
		this.onopen = onopen;
	}
	public String getOnclose() {
		return onclose;
	}
	public void setOnclose(String onclose) {
		this.onclose = onclose;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getHeight() {
		return height;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getShowtitle() {
		return showtitle;
	}
	public void setShowtitle(String showtitle) {
		this.showtitle = showtitle;
	}
	public String getStopclose() {
		return stopclose;
	}
	public void setStopclose(String stopclose) {
		this.stopclose = stopclose;
	}
}
